﻿using System.Linq;
using Castle.MonoRail.Framework;
using Castle.MonoRail.Framework.Filters;
using StateTheater.Models;
using StateTheaterDataModel;


namespace StateTheater.Controllers
{
	public class NameCount : NameDetails
	{
		public int Count { get; set; }
	}
	[DefaultAction]
	[Layout("default"), Rescue("generalerror")]
	[Filter(ExecuteWhen.BeforeAction, typeof(MobileFilter))]
	public class StatisticController : SmartDispatcherController
	{
		StateTheaterDataContext db;
		public StatisticController(StateTheaterDataContext db)
		{
			this.db = db;
		}
		public void MostActiveActors()
		{
//            StateTheaterDataContext db = new StateTheaterDataContext(Util.ConnectionString);
			db.Log = new Castle.MonoRail.Framework.Internal.ProperyBagTextWriter("text2", PropertyBag);
			var q2 = from c in
						 (
						from p in db.Peoples
						select new NameCount
							   {
//                                   FullName = p.FullName,
								   FirstName = p.FirstName,
								   LastName = p.LastName,
								   ID = p.PersonID,
								   HasPhoto = p.Photo==null ? false : p.Photo.Length > 0,
								   HasBio = p.Bio == null ? false : p.Bio.Length > 0,
								   Count = (from rrr in db.Roles where rrr.PersonID == p.PersonID select rrr).Count()
							   })
					 where c.Count > 5
					 orderby c.Count descending, c.LastName ascending, c.FirstName ascending
					 select c;

			var elem = q2.ToList();
			elem.ForEach(nd => nd.SEOName = Util.SEOEscape(nd.FullName, false));
			PropertyBag["elements"] = elem;
			
//            PropertyBag["text"] = db.GetQueryText(q2);

			PropertyBag["title"] = "Most Active Actors";

			RenderView("list");
		}

		public void MostPopularAuthors()
		{
			var q2 = from pr in db.Productions
					 join pc in db.PlayCredits on pr.PlayID equals pc.PlayID
					 group pr by pc.People into shows
					 let cnt = shows.Count()
					 where cnt > 6
					 orderby cnt descending
					 select new NameCount
					 {
						 FirstName = shows.Key.FirstName,
						 LastName = shows.Key.LastName,
						 ID = shows.Key.PersonID,
						 HasPhoto = shows.Key.Photo == null ? false : shows.Key.Photo.Length > 0,
						 HasBio = shows.Key.Bio == null ? false : shows.Key.Bio.Length > 0,
						 Count = cnt
					 };

			var elem = q2.ToList();
			elem.ForEach(nd => nd.SEOName = Util.SEOEscape(nd.FullName, false));
			PropertyBag["elements"] =elem;

			PropertyBag["title"] = "Most Produced Authors";

			RenderView("list");
		}
		public void MostPopularAuthorsUnique()
		{
//            StateTheaterDataContext db = new StateTheaterDataContext(Util.ConnectionString);
//			db.Log = newCastle.MonoRail.Framework.Internal.ProperyBagTextWriter("text2", PropertyBag);
			var q2 = from c in
						 (
						from p in db.Peoples
						select new NameCount
						{
//							FullName = p.FullName, 
							FirstName = p.FirstName,
							LastName = p.LastName,
							ID = p.PersonID,
							HasPhoto = p.Photo == null ? false : p.Photo.Length > 0,
							HasBio = p.Bio == null ? false : p.Bio.Length > 0,
							Count = (from rrr in db.PlayCredits  where rrr.PersonID == p.PersonID select rrr).Count()
						})
					 where c.Count > 5
					 orderby c.Count descending, c.LastName ascending, c.FirstName ascending
					 select c;

			var elem = q2.ToList();
			elem.ForEach(nd => nd.SEOName = Util.SEOEscape(nd.FullName, false));
			PropertyBag["elements"] = elem;

			PropertyBag["title"] = "Most Different Plays by an Author";

			RenderView("list");
		}

		public void MostPopularPlays()
		{
			var q2 = from p in db.Productions
					 group p by p.Play into plays
					 let cnt = plays.Count()
					 where cnt > 7
					 orderby cnt descending
					 select new
					 {
						 plays.Key.Title,
						 ID= plays.Key.PlayID,
						 Count = cnt
					 };

			PropertyBag["elements"] = q2.ToList();

			PropertyBag["title"] = "Most Produced Plays";

			RenderView("listplay");
		}

		private void MostActiveCraft(string title, params int[] crafts)
		{
//            StateTheaterDataContext db = new StateTheaterDataContext(Util.ConnectionString);
//			db.Log = new Castle.MonoRail.Framework.Internal.ProperyBagTextWriter("text2", PropertyBag);
			var q2 = from c in
						 (
						from p in db.Peoples
						select new NameCount
						{
//							FullName = p.FullName,
							FirstName = p.FirstName,
							LastName = p.LastName,
							ID = p.PersonID,
							HasPhoto = p.Photo == null ? false : p.Photo.Length > 0,
							HasBio = p.Bio == null ? false : p.Bio.Length > 0,
							Count = (from rrr in db.ProductionCredits where crafts.Contains(rrr.CreditID.Value) && rrr.PersonID == p.PersonID select rrr).Count()
						})
					 where c.Count > 4
					 orderby c.Count descending, c.LastName ascending, c.FirstName ascending
					 select c;

			var elem = q2.ToList();
			elem.ForEach(nd => nd.SEOName = Util.SEOEscape(nd.FullName, false));
			PropertyBag["elements"] = elem;
			PropertyBag["title"] = title;
			RenderView("list");
		}
		public void MostActiveDirectors()
		{
			MostActiveCraft("Most Active Directors", 100);
		}
		public void MostActiveProducers()
		{
			MostActiveCraft("Most Active Producers", 107);
		}
		public void MostActiveChoreographers()
		{
			MostActiveCraft( "Most Active Choreographers", 105, 110);
		}
		public void MostActiveSetDesigners()
		{
			MostActiveCraft("Most Active Set Designers", 115, 190, 191, 192);
		}
		public void MostActiveMusicalDirectors()
		{
			MostActiveCraft("Most Active Musical Directors", 170);
		}
		public void MostActiveStageManagers()
		{
			MostActiveCraft("Most Active Stage Managers", 160,162);
		}
		public void MostActiveLightingDesigners()
		{
			MostActiveCraft("Most Active Lighting Designers", 130);
		}
		public void MostActiveSoundDesigners()
		{
			MostActiveCraft("Most Active Sound Designers", 140);
		}
		public void MostActiveCostumers()
		{
			MostActiveCraft("Most Active Costumers", 200, 120);
		}

		public void LargestCasts()
		{
			var q2 = from p in db.Productions
					 let cnt = p.Roles.Count
					 where cnt > 30
					 orderby cnt descending
					 select new
						 {
							 Count = cnt,
							 ID = p.ProductionID,
							 Title = p.Play.Title,
							 TroupeName = p.Troupe.Name,
							 ShortName = p.Troupe.shortname
						 };

			PropertyBag["elements"] = q2.ToList();

			PropertyBag["title"] = "Largest Casts";

			RenderView("listshow");
		}      

	}
}
